/* tslint:disable */
var createError = require("http-errors");
import express from "express";
import { Request, Response } from "express";
import { NextFunction } from "express-serve-static-core";
import compression from "compression"; // compresses requests
import bodyParser from "body-parser";
import dotenv from "dotenv";
var cookieParser = require("cookie-parser");

// import flash from 'express-flash';
import path from "path";
import mongoose from "mongoose";

import passport from "passport";
import expressValidator from "express-validator";
import bluebird from "bluebird";

import firewall from "../firewall";

import * as config from "../config";

import sliderCode from "./routes/sliderCode";
import userRouter from "./routes/user";
import configRouter from "./routes/config";
import phoneCodeRouter from "./routes/phoneCode";
import emailCodeRouter from "./routes/emailCode";
import fileRouter from "./routes/file";
import goodRouter from "./routes/good";
import qouteRouter from "./routes/quote";
import symbolRouter from "./routes/symbol";
import assetsRouter from "./routes/assets";
import tradeRouter from "./routes/trade";
import bugRouter from "./routes/bug";
// Load environment variables from .env file, where API keys and passwords are configured
// dotenv.config({ path: ".env" });

const cors = require("cors");
// var proxy = require("http-proxy-middleware");
var proxy = require("express-http-proxy");

// Create Express server
const app = express();

app.use(
  cors({
    // origin: 'http://www.zqd.com',
    origin: [
      config.domainC,
      "http://www.newagirl.com",
      "http://www.elitex.space",
      "https://www.elitex.space",
      "http://backend.elitex.space",
      "https://www.elitex.io",
      "http://www.elitex.space:83",
      "https://backend.elitex.io"
    ],
    credentials: true,
    methods: ["OPTIONS", "GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"]
  })
);

app.use(cookieParser());
app.use(compression());
// Express configuration
app.set("port", process.env.PORT || 3000);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());

const fileUpload = require("express-fileupload");

app.use(
  fileUpload({
    limits: { fileSize: 50 * 1024 * 1024 }
  })
);

// function proxyApi() {
//   return proxy("http://192.168.30.22:5000", {
//     filter,
//     proxyReqPathResolver: function(req: any) {
//       if (req.url === "/config") {
//         const parts = req.url.split("?");
//         const queryString = parts[1];
//         const updatedPath = "/config";
//         return updatedPath + (queryString ? "?" + queryString : "");
//       }
//       if (req.url === "/sliderCode/register-fullpage") {
//         const parts = req.url.split("?");
//         const queryString = parts[1];
//         const updatedPath = "/sliderCode/register-fullpage";
//         return updatedPath + (queryString ? "?" + queryString : "");
//       }
//       return req.url;
//     },
//     proxyReqOptDecorator: function(proxyReqOpts: any, originalReq: any) {
//       return proxyReqOpts;
//     },
//     // 请求体 用于post
//     proxyReqBodyDecorator: function(bodyContent: any, srcReq: any) {
//       if (srcReq.url === "/user/login") {
//       }
//       return bodyContent;
//     },
//     userResDecorator: function(
//       proxyRes: any,
//       proxyResData: any,
//       userReq: any,
//       userRes: any
//     ) {
//       const data = JSON.parse(proxyResData.toString("utf8"));
//       data.newProperty = "exciting data";
//       return JSON.stringify(data);
//     }
//   });
// }

// function proxyEhApi() {
//   return proxy("http://api.zqd.com", {
//     filter,
//     proxyReqPathResolver: function(req: any) {
//       if (req.url === "/config") {
//         const parts = req.url.split("?");
//         const queryString = parts[1];
//         const updatedPath = "/config";
//         return updatedPath + (queryString ? "?" + queryString : "");
//       }
//       if (req.url === "/sliderCode/register-fullpage") {
//         const parts = req.url.split("?");
//         const queryString = parts[1];
//         const updatedPath = "/sliderCode/register-fullpage";
//         return updatedPath + (queryString ? "?" + queryString : "");
//       }
//       return req.url;
//     },
//     proxyReqOptDecorator: function(proxyReqOpts: any, originalReq: any) {
//       return proxyReqOpts;
//     },
//     // 请求体 用于post
//     proxyReqBodyDecorator: function(bodyContent: any, srcReq: any) {
//       if (srcReq.url === "/user/login") {
//       }
//       return bodyContent;
//     },
//     userResDecorator: function(
//       proxyRes: any,
//       proxyResData: any,
//       userReq: any,
//       userRes: any
//     ) {
//       const data = JSON.parse(proxyResData.toString("utf8"));
//       data.newProperty = "exciting data";
//       return JSON.stringify(data);
//     }
//   });
// }

// var filter = function(req: any, res: any) {
//   return req.method === "GET" || req.method === "POST";
// };

// app.use("/", proxyApi());

app.use("/quote", qouteRouter);
app.use("/user", userRouter);
app.use("/config", configRouter);

app.use("/phoneCode", phoneCodeRouter);
app.use("/emailCode", emailCodeRouter);
app.use("/file", fileRouter);
app.use("/good", goodRouter);
app.use("/sliderCode", sliderCode);
app.use("/symbol", symbolRouter);
app.use("/assets", assetsRouter);
app.use("/trade", tradeRouter);
app.use("/bug", bugRouter);

///////////// backend //////////////////////

import userRouterBe from "./routesBe/user";
import configRouterBe from "./routesBe/config";
import fileRouterBe from "./routesBe/file";
import symbolRouterBe from "./routesBe/symbol";
import assetsRouterBe from "./routesBe/assets";
import exchangeRouterBe from "./routesBe/exchange";

app.use("/be/user", userRouterBe);
app.use("/be/config", configRouterBe);
app.use("/be/file", fileRouterBe);
app.use("/be/symbol", symbolRouterBe);
app.use("/be/assets", assetsRouterBe);
app.use("/be/exchange", exchangeRouterBe);

app.use("*", firewall);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err: any, req: Request, res: Response, next: NextFunction) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};
  // render the error page
  res.status(err.status || 500);
  res.send(err.status);
});

export default app;
